import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useAppSelector } from './useStore';

export const useAuthGuard = () => {
  const navigate = useNavigate();
  const location = useLocation();
  const { data } = useAppSelector(state => state.user);

  useEffect(() => {
    if (!data?.login && location.pathname !== '/login') {
      // 保存当前路径到 localStorage 或 URL 参数
      localStorage.setItem('preRoute', location.pathname);
      navigate('/login');
    }
  }, [data, navigate, location]);
};
